#include "cloud_attribute.h"


#define DISCOVER "discover"
#define BEGIN "begin"
#define STOP "stop"
#define OTABEGIN "OTAbegin"
#define OTASTOP "OTAstop"

/*用于解析串口下发的高速数据*/
void parse_uart_play_data(u8 *g_uart_buf)
{

    spi_send_param *spi_param = NULL;
    u8 *uart_rx_buffer = NULL;
    spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    //printf("play data is %d\n",(g_uart_buf[1]<<8 |g_uart_buf[2]));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc create spi_param err\n");
        return;
    }
    uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(g_uart_buf[1]<<8 |g_uart_buf[2]));
    if(uart_rx_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc create uart_rx_buffer err\n");
        return;
    }
    
    memcpy(uart_rx_buffer,&g_uart_buf[3],(g_uart_buf[1]<<8 |g_uart_buf[2]));

    spi_param->send_len = (g_uart_buf[1]<<8 |g_uart_buf[2])-3; //待发送数据长度
    spi_param->send_port = g_uart_buf[0]; //待发送数据指定的灯带
    spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
    
    xQueueSend(spi_queue_handle,&spi_param,0);
    uart_rx_buffer =NULL; //防止野指针
    spi_param = NULL;

    cnt =0;
    state_num = STATE_PLAY; //进入高速模式

}

/*用于解析串口下发的高速数据*/
void parse_uart_discover_data(u8 *g_uart_buf)
{
    //if(strncmp((char *)g_uart_buf,DISCOVER,8) == 0)
    {
        pack_regiser_data(); //上报，注册函数
    }
}

/*高速模式开始*/
void parse_uart_begin_data(u8 *g_uart_buf)
{
    //if(strncmp((char *)g_uart_buf,BEGIN,5) == 0)
    {
        cnt =0;
        state_num = STATE_PLAY; //进入高速模式
        pack_uart_into_data();
        
        printf("STATE_PLAY\n");
        disp_clear_light_corner();
        disp_clear_light_around();
        disp_clear_light_front();
    }
}

/*高速模式关闭*/
void parse_uart_stop_data(u8 *g_uart_buf)
{
    //if(strncmp((char *)g_uart_buf,STOP,4) == 0)
    {
        cnt =500;
    }
}

/*OTA模式开始数据包*/
void parse_uart_OTAbegin_data(u8 *g_uart_buf)
{
    //if(strncmp((char *)g_uart_buf,OTABEGIN,7) == 0)
    {
        OTA_init();
        pack_uart_OTA_into_data();
        state_num = STATE_OTA; //进入高速模式
        cnt =0;
    }
}


/*OTA模式结束数据包*/
void parse_uart_OTAstop_data(u8 *g_uart_buf)
{
    //if(strncmp((char *)g_uart_buf,OTASTOP,7) == 0)
    {
        OTA_finish();
    }
}

static void print_data_hex(uint8_t *data, u16 len)
{   
    printf("------> data: cloud");
    for (int i = 0; i < len; i++) 
    {
        printf(" 0x%x", data[i]);
    }
    
    printf(", len1: %d\n", len);
}

/*OTA升级数据包*/
void parse_uart_OTA_data(u8 *g_uart_buf)
{   
    //print_data_hex(&g_uart_buf[2],(g_uart_buf[0]<<8 | g_uart_buf[1])-2);
    cnt=0;
    OTA_write(&g_uart_buf[2],(g_uart_buf[0]<<8 | g_uart_buf[1])-2);
}

void parse_cloud_data(u8 *g_uart_buf,u16 length)
{
    //print_data_hex(g_uart_buf,length);
    if((g_uart_buf[0] == 0x41)&&(g_uart_buf[1] == 0x52)&&(g_uart_buf[2] == 0x54)) //校验包头
    {
        printf("%d\n",(g_uart_buf[4]<<8 | g_uart_buf[5]));        
        if(length ==(g_uart_buf[4]<<8 | g_uart_buf[5]))
        {
            switch (g_uart_buf[3]) //校验命令帧，去不同的函数
            {
            case UART_DISCOVER: //云端发现包
                parse_uart_discover_data(&g_uart_buf[6]);
                break;
            case UART_BEGIN: //高速模式开始包
                parse_uart_begin_data(&g_uart_buf[6]);
                break;
            case UART_STOP: //高速模式停止包
                parse_uart_stop_data(&g_uart_buf[6]);
                break;
            case UART_PLAY: //高速模式数据包
                parse_uart_play_data(&g_uart_buf[6]);
                break;
            case UART_OTA_BEGIN: //OTA模式开始包
                parse_uart_OTAbegin_data(&g_uart_buf[6]);
                break;
            case UART_OTA_STOP: //OTA模式结束包
                parse_uart_OTAstop_data(&g_uart_buf[6]);
                break;
            case UART_OTA_DOWNLOAD: //OTA模式下载包
                parse_uart_OTA_data(&g_uart_buf[6]);
                break;                                                              
            default:
                LOG_ERROR("Undefined command\n");
                break;
            }
        }
        else
        {
            LOG_ERROR("Length check error\n");
        }
    }
    else
    {
        LOG_INFO("Packet receiving error\n");
    }
}



